﻿using Kingdee.BOS;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Mobile.Contract;
using Kingdee.BOS.Mobile.DataEnties;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Security;
using System.Security.AccessControl;
using System.Security.Policy;
using System.Text;

namespace OtherPlatformMessage
{
    [HotUpdate]
    public class Message : IOtherPlatformMessage
    {
        public MobileResponse Send(Context ctx, MobileMessage message)
        {
            using (FileStream fileStream = new FileStream("D:\\messagetest\\msg.txt", FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fileStream))
                {
                    // 写入内容到文件。    
                    sw.WriteLine("待办任务标题: ");
                }
            }

            // 待办任务标题  
            string title = message.Title;
            // 待办任务内容  
            string text = message.Text;
            // 消息类型  
            string source_type = message.SourceType;
            // 待办任务ID
            string source_id = message.SourceId;
            // 消息状态枚举，待办todo=1；已办done=2  
            string status = message.Status.ToString();
            // 接受者用户列表  
            List<string> users = message.Users;

            // 获取用户手机号
            List<string> fphone_list = new List<string>();
            if (users != null && users.Count > 0) {
                foreach (var userid in users)
                {
                    var fphone_res = DBServiceHelper.ExecuteDynamicObject(ctx, @"select FPHONE from T_SEC_USER where FUSERID = '" + userid + "'");
                    if (fphone_res != null) {
                        if (fphone_res[0][0].ToString() != null && fphone_res[0][0].ToString() != "") {
                            fphone_list.Add(fphone_res[0][0].ToString());
                        }
                    }
                }
            }

            RequestParams rp = new RequestParams()
            {
                 title = title,
                 text = text,
                 sourceType = source_type,
                 sourceId = source_id,
                 status = status == "todo" ? 1 : (status == "done" ? 2 : 3),
                 userPhones = fphone_list
            };

            String res = HttpPost("http://49.72.51.235:8090/api/juffer/zj_xnhb_out/k3cloudMsgToOA",JsonConvert.SerializeObject(rp));


            using (FileStream fileStream = new FileStream("D:\\messagetest\\msg.txt", FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fileStream))
                {
                    // 写入内容到文件。    
                    sw.WriteLine("请求结果: ----------------------------------" );
                    sw.WriteLine("待办任务标题: " + title);
                    sw.WriteLine("待办任务内容: " + text);
                    sw.WriteLine("消息类型: " + source_type);
                    sw.WriteLine("待办任务ID: " + source_id);
                    sw.WriteLine("消息状态: " + status.ToString());
                    sw.WriteLine("接受者用户列表: " + string.Join(", ", users));
                    sw.WriteLine("接受者用户列表手机号: " + string.Join(", ", fphone_list));
                    sw.WriteLine("请求结果: " + res);
                }
            }

            var response = new MobileResponse
            {
                Errcode = 0,
                Errmsg = "Success"
            };

            return response;
        }

        public static string HttpPost(string url, string str)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.CookieContainer = new CookieContainer();
                request.ContentType = "application/json";//"application/x-www-form-urlencoded";
                Encoding encoding = Encoding.UTF8;
                request.Method = "post";
                Stream requestStream = request.GetRequestStream();
                if (str != "")
                {
                    byte[] postData = encoding.GetBytes(str);
                    requestStream.Write(postData, 0, postData.Length);
                }
                var response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
                string retString = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                return retString;
            }
            catch (Exception e)
            {
                throw new Exception("POST请求出现异常", e);
            }
        }
    }

    public class RequestParams {

        public String title { get; set; }
        public String text { get; set; }
        public String sourceType { get; set; }
        public String sourceId { get; set; }
        public int status { get; set; }
        public List<String> userPhones { get; set; }
    }
}